Telegram Group & Telegram Channel
Очищаем строки

Чаще всего обработка входных данных сводится к преобразованию символов в верхний или нижний регистр. Иногда данные можно очистить с помощью регулярного выражения. 

Но в случаях, когда задача усложняется, можно применить более удачный способ её решения. С помощью словаря со значениями для замены символов и метода translate получается крайне лаконичный код.

В примере видим, как символы переноса строки "\n" и табуляции "\t" заменяются на обычные пробелы, а символ "\r" удаляется из строки полностью.

#строки



tg-me.com/python_academy/536
Create:
Last Update:

Очищаем строки

Чаще всего обработка входных данных сводится к преобразованию символов в верхний или нижний регистр. Иногда данные можно очистить с помощью регулярного выражения. 

Но в случаях, когда задача усложняется, можно применить более удачный способ её решения. С помощью словаря со значениями для замены символов и метода translate получается крайне лаконичный код.

В примере видим, как символы переноса строки "\n" и табуляции "\t" заменяются на обычные пробелы, а символ "\r" удаляется из строки полностью.

#строки

BY Python Academy




Share with your friend now:
tg-me.com/python_academy/536

View MORE
Open in Telegram


Python Academy Telegram | DID YOU KNOW?

Date: |

The lead from Wall Street offers little clarity as the major averages opened lower on Friday and then bounced back and forth across the unchanged line, finally finishing mixed and little changed.The Dow added 33.18 points or 0.10 percent to finish at 34,798.00, while the NASDAQ eased 4.54 points or 0.03 percent to close at 15,047.70 and the S&P 500 rose 6.50 points or 0.15 percent to end at 4,455.48. For the week, the Dow rose 0.6 percent, the NASDAQ added 0.1 percent and the S&P gained 0.5 percent.The lackluster performance on Wall Street came on uncertainty about the outlook for the markets following recent volatility.

Among the actives, Ascendas REIT sank 0.64 percent, while CapitaLand Integrated Commercial Trust plummeted 1.42 percent, City Developments plunged 1.12 percent, Dairy Farm International tumbled 0.86 percent, DBS Group skidded 0.68 percent, Genting Singapore retreated 0.67 percent, Hongkong Land climbed 1.30 percent, Mapletree Commercial Trust lost 0.47 percent, Mapletree Logistics Trust tanked 0.95 percent, Oversea-Chinese Banking Corporation dropped 0.61 percent, SATS rose 0.24 percent, SembCorp Industries shed 0.54 percent, Singapore Airlines surrendered 0.79 percent, Singapore Exchange slid 0.30 percent, Singapore Press Holdings declined 1.03 percent, Singapore Technologies Engineering dipped 0.26 percent, SingTel advanced 0.81 percent, United Overseas Bank fell 0.39 percent, Wilmar International eased 0.24 percent, Yangzijiang Shipbuilding jumped 1.42 percent and Keppel Corp, Thai Beverage, CapitaLand and Comfort DelGro were unchanged.

Python Academy from ru


Telegram Python Academy
FROM USA